import django.http as http
import django.shortcuts as shortcuts
from django.template import RequestContext
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.core.urlresolvers import reverse
import django.utils

import django_utils.pagination as pagination
import django_utils.request_helpers as request_helpers

def browse_ajax_placeholder(request):
    page = request_helpers.get_page(request)
    tags = models.MetaTag.objects.all().order_by('name')
    form = forms.TagSearchForm()
    
    paginator = pagination.DiggPaginator(tags, 10, body=5)
    current_page = paginator.page(page)
    page_range = paginator.page_range
    object_count = len(current_page.object_list)
    
    for tag in current_page.object_list:
        tag.count = models.Tag.objects.filter(tag = tag).count()
        tag.safe_name = django.utils.http.urlquote(tag.name)
        
    return shortcuts.render_to_response(
        'taxanomy/browse_ajax.html', 
        {'current_page':current_page,  'page_range':page_range,   'form':form}, 
        context_instance = RequestContext(request), 
    )
    
def retag(request,  content_type,  object_id):
    user = community_helpers.get_logged_user(request)

    if user:
        target_model = ContentType.objects.get(id = content_type)
        target_object = target_model.model_class().objects.get(id = object_id)
        template_name = "taxanomy/standard_retag.html"
        if request.POST:
            tag_form = forms.TagForm(request.POST)
            tag_form.target_object = target_object
            if tag_form.is_valid():
                target_object.add_tag(helpers.parse_tag(request.POST['tag']),  user)
        else:
            tag_form = forms.TagForm() 
        tags = target_object.tags.all()
        for tag in tags:
            tag.set_safe()
        return shortcuts.render_to_response(
            template_name, 
            {'node':target_object, 'content_type':content_type,  'object_id':object_id,  'tags':tags,  'tag_form':tag_form}, 
            context_instance = RequestContext(request), 
        )
    else:
        template_name = 'util/error.html'
        return shortcuts.render_to_response(
            template_name, 
            {}, 
            context_instance = RequestContext(request), 
        )

def remove_tag(request,  content_type,  object_id,  tag):
    user = community_helpers.get_logged_user(request)
    
    content_type_object = ContentType.objects.get(id = content_type)
    target_object = content_type_object.model_class().objects.get(id = object_id)
    
    try:
        metatag = models.MetaTag.objects.get(name = tag)
        matching_tags = models.Tag.objects.filter(content_type = content_type_object,  object_id = target_object.id,  tag = metatag)
        for tag in matching_tags:
            tag.log_delete(user)
    except ObjectDoesNotExist:
        pass
        
    return http.HttpResponseRedirect(reverse('retag',  args=[content_type, object_id]))
        
def tagged(request,  tag,  model):
    page = request_helpers.get_page(request)
    
    if model == 'questions':
        tagged_objects = Question.get_tagged_items(Question,  tag)
    if model == 'techniques':
        tagged_objects = Technique.get_tagged_items(Technique,  tag)
    if model == 'widgets':
        tagged_objects = Widget.get_tagged_items(Widget,  tag)
    if model == 'frameworks':
        tagged_objects = Framework.get_tagged_items(Framework,  tag)
    if model == 'tools':
        tagged_objects = Tool.get_tagged_items(Tool,  tag)
    
    paginator = pagination.DiggPaginator(tagged_objects, 10, body=5)
    current_page = paginator.page(page)
    page_range = paginator.page_range
    object_count = len(current_page.object_list)
    
    safe_tag = django.utils.http.urlquote(tag)
    
    return shortcuts.render_to_response(
        'taxanomy/tagged_content.html', 
        {'current_page':current_page,  'page_range':page_range,  'model':model,  'tag':tag,  'safe_tag':safe_tag}, 
        context_instance = RequestContext(request), 
    )

def tagged_model(request,  tag,  model='questions'):
    page = request_helpers.get_page(request)
    sort = request_helpers.get_sort(request,  'highest_rated')
    
    if model == 'questions':
        tagged_objects = Question.get_tagged_items(Question,  tag,  sort)
    if model == 'techniques':
        tagged_objects = Technique.get_tagged_items(Technique,  tag,  sort)
    if model == 'widgets':
        tagged_objects = Widget.get_tagged_items(Widget,  tag,  sort)
    if model == 'frameworks':
        tagged_objects = Framework.get_tagged_items(Framework,  tag,  sort)
    if model == 'tools':
        tagged_objects = Tool.get_tagged_items(Tool,  tag)
        
    current_page,  page_range = pagination.paginate_queryset(tagged_objects,  10,  5,  page)
    safe_tag = django.utils.http.urlquote(tag)
    
    return shortcuts.render_to_response(
        'taxanomy/tagged_model.html', 
        {'current_page':current_page,  'page_range':page_range,  'model':model,  'tag':tag,  'safe_tag':safe_tag,  'sort':sort,  'model':model}, 
        context_instance = RequestContext(request), 
    )
    
def tagged_content_type(request,  tag,  content_type):
    page = request_helpers.get_page(request)
    sort = request_helpers.get_sort(request,  'highest_rated')
    
    content_type_object = ContentType.objects.get(id = content_type)
    model = content_type_object.model_class().__name__.lower()
    
    if model == 'question':
        tagged_objects = Question.get_tagged_items(Question,  tag,  sort)
    elif model == 'technique':
        tagged_objects = Technique.get_tagged_items(Technique,  tag,  sort)
    elif model == 'widget':
        tagged_objects = Widget.get_tagged_items(Widget,  tag,  sort)
    elif model == 'framework':
        tagged_objects = Framework.get_tagged_items(Framework,  tag,  sort)
    elif model == 'tool':
        tagged_objects = Tool.get_tagged_items(Tool,  tag)
        
    current_page,  page_range = pagination.paginate_queryset(tagged_objects,  10,  5,  page)
    safe_tag = django.utils.http.urlquote(tag)
    
    return shortcuts.render_to_response(
        'taxanomy/tagged_content_type.html', 
        {'current_page':current_page,  
            'page_range':page_range,  'model':model,  'tag':tag,  'safe_tag':safe_tag,  'sort':sort,  'model':model,  'content_type':content_type}, 
        context_instance = RequestContext(request), 
    )